package nextEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type SourceEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	Key string // 公司名称

	Value map[string]interface{} // 配置参数
}

type sourcePo struct {
	Id         int64 `json:"id" db:"id"`                  // 对象id
	CreateTime int64 `json:"createTime" db:"create_time"` // 创建时间
	UpdateTime int64 `json:"updateTime" db:"update_time"` // 更新时间

	Key   string                 `json:"key" db:"key"`
	Value map[string]interface{} `json:"value" db:"value"`
}

func (e *SourceEntity) Type() string {
	return "SourceEntity"
}

func (e *SourceEntity) TableName() string {
	return "tb_source_xx"
}

func (e *SourceEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *SourceEntity) Instance() *SourceEntity {
	return &SourceEntity{
		BaseEntity: new(commEntity.BaseEntity),
		Value:      make(map[string]interface{}),
	}
}

func (e *SourceEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *SourceEntity) NewPo() interface{} {
	return &sourcePo{
		Value: make(map[string]interface{}),
	}
}

func (e *SourceEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *SourceEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.Key)
	return p
}

func (e *SourceEntity) BuildByJson(jsn *string) error {
	var po sourcePo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *SourceEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *SourceEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*sourcePo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime

	ptr.Value = po.Value
	ptr.Key = po.Key
}

func (e *SourceEntity) BuildToPo() interface{} {
	ptr := e
	po := sourcePo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime

	po.Value = ptr.Value
	po.Key = ptr.Key

	return &po
}
